1 /*
2  * Hunt - a framework for web and console application based on Collie using Dlang development
3  *
4  * Copyright (C) 2015-2017  Shanghai Putao Technology Co., Ltd
5  *
6  * Developer: HuntLabs
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.view.templerender;
13 
14 public import hunt.view;
15 
16 class View
17 {
18 private:
19 	TempleContext _context;
20 	CompiledTemple _layout;
21 	bool _setLayout = false;
22 
23 public:
24 
25 	TempleContext context()
26 	{
27 		if(_context is null)
28 		{
29 			_context = new TempleContext();
30 		}
31 		return _context;
32 	}
33 
34 	void setLayout(string filename = null)()
35 	{
36 		_setLayout = true;
37 		_layout = compile_temple_file!filename;
38 	}
39 
40 	void opIndexAssign(T)(string name, T val)
41 	{
42 		context[name] = val;
43 	}
44 
45 	void opDispatch(string name, T)(T val)
46 	{
47 		context[val] = name;
48 	}
49 
50 	string render(string filename = null)()
51 	{
52 		auto child = compile_temple_file!filename; 
53 		if(_setLayout)
54 		{
55 			_setLayout = false;
56 			auto composed = _layout.layout(&child);
57 			return composed.toString(context);
58 		}
59 		else
60 		{
61 			return child.toString(context);
62 		}
63 	}
64 
65 	string show(string filename = null)()
66 	{
67 		auto child = compile_temple_file!filename; 
68 		return child.toString(context);
69 	}
70 }